using Godot;
using System;
using System.Collections.Generic;

public partial class LevelRoundTalk : Node2D
{
    [Signal]
    public delegate void OnRoundTalkEndEventHandler();
    CanvasLayer canvasLayer;
    CanvasLayer canvasLayerMask;
    PanelContainer panel;
    Marker2D marker2DTarget;
    Marker2D mt1;
    Marker2D mt2;
    Marker2D mb1;
    Marker2D mb2;
    Polygon2D polygon2D;

    TextureRect pilotAvatar;
    Label actorName;
    Label words;
    Button buttonContinue;
    int screenWidth;
    int screenHeight;

    public override void _Ready()
    {
        canvasLayer = GetNode<CanvasLayer>("CanvasLayer");
        canvasLayerMask = GetNode<CanvasLayer>("CanvasLayerMask");
        panel = GetNode<PanelContainer>("CanvasLayer/Panel");
        buttonContinue = GetNode<Button>("CanvasLayerMask/ButtonContinue");
        buttonContinue.Pressed += OnContinueHandler;
        marker2DTarget = GetNode<Marker2D>("Marker2DTarget");
        mt1 = GetNode<Marker2D>("CanvasLayer/Panel/MT1");
        mt2 = GetNode<Marker2D>("CanvasLayer/Panel/MT2");
        mb1 = GetNode<Marker2D>("CanvasLayer/Panel/MB1");
        mb2 = GetNode<Marker2D>("CanvasLayer/Panel/MB2");
        polygon2D = GetNode<Polygon2D>("CanvasLayer/Polygon2D");
        pilotAvatar = GetNode<TextureRect>("%PilotAvatar");
        actorName = GetNode<Label>("%ActorName");
        words = GetNode<Label>("%Words");

        screenWidth = (int)GetViewportRect().Size.X;
        screenHeight = (int)GetViewportRect().Size.Y;
        halfScreen = screenHeight / 2;
    }

    private void OnContinueHandler()
    {
        if (GameManager.Instance.IsLockInput) return;
        talkIndex++;
        if (talkIndex >= talkWords.Count)
        {
            talkIndex = 0;
            canvasLayer.Hide();
            canvasLayerMask.Hide();
            EmitSignal(SignalName.OnRoundTalkEnd);
        }
        else
        {
            UpdateStory();
        }
    }


    List<TalkModel> talkWords;
    private int talkIndex = 0;
    BaseMapManager map;
    RobotManager rmg;
    Cursor cursor;

    Vector2[] ps = new Vector2[3];
    int halfScreen;
    public void AlignActor(Vector2 pos)
    {
        marker2DTarget.Position = pos;
        var x = pos.X - 250;
        x = Mathf.Clamp(x, 50, screenWidth - 550);
        var y = pos.Y > halfScreen ? pos.Y - 190 : pos.Y + 50;
        panel.Position = new Vector2(x, y);
        if (pos.Y > halfScreen)
        {
            // 在下 取下两点
            ps[0] = mb1.GlobalPosition;
            ps[1] = mb2.GlobalPosition;
        }
        else
        {
            // 在上 取上两点
            ps[0] = mt1.GlobalPosition;
            ps[1] = mt2.GlobalPosition;
        }

        ps[2] = marker2DTarget.GlobalPosition;
        polygon2D.Polygon = ps;
    }

    public void StartPlay(List<TalkModel> list)
    {
        map = GameManager.Instance.LevelManager.MapManager;
        rmg = GameManager.Instance.LevelManager.RobotManager;
        cursor = GameManager.Instance.LevelManager.Cursor;
        talkWords = list;
        canvasLayerMask.Show();
        UpdateStory();
    }

    async void UpdateStory()
    {
        var cfg = talkWords[talkIndex];
        var robot = rmg.GetRobotByPilot(cfg.Avatar);
        if (robot != null)
        {
            cursor.FocusTarget(robot);
            canvasLayer.Hide();
            await ToSignal(cursor, nameof(Cursor.CameraMoveEnd));
        }
        canvasLayer.Show();
        actorName.Text = cfg.ActorName;
        words.Text = cfg.Word;
        pilotAvatar.Texture = GameUtil.GetPilotAvatar(cfg.Avatar);
        if (robot != null)
        {
            AlignActor(robot.GetGlobalTransformWithCanvas().Origin);
        }
    }
}
